package com.tong.hao.ac.service.component.finance;

import cn.buddha.jec.model.TongContext;
import cn.buddha.jec.util.TongBeanUtils;
import cn.buddha.jec.util.TongBusinessUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tong.hao.ac.db.entity.AcBillFlow;
import com.tong.hao.ac.db.entity.AcTest;
import com.tong.hao.ac.db.entity.AcUser;
import com.tong.hao.ac.db.service.IAcBillFlowService;
import com.tong.hao.ac.db.service.IAcTestService;
import com.tong.hao.ac.db.service.IAcUserService;
import com.tong.hao.ac.service.domain.finance.AcBillReqDo;
import com.tong.hao.common.exception.HaoExceptionEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName AcBillComponent
 * @Author 友野浩二
 * @Date 2020/4/27 9:49
 * @Description 账单组件
 * @Version 1.0
 */
@Component
public class AcBillComponent {

    /**
     * 用户信息 服务类
     */
    @Resource
    private IAcUserService acUserServiceImpl;

    /**
     * 账单记录 服务类
     */
    @Resource
    private IAcBillFlowService acBillFlowServiceImpl;

    /**
     * 测试 服务类
     */
    @Resource
    private IAcTestService acTestServiceImpl;

    /**
     * @return
     * @Author 友野浩二
     * @Description // 账单登记
     * @Date 2020/5/3 15:52
     * @Param [acBillReqDo]
     */
    public void registerAcBill(AcBillReqDo acBillReqDo) {
        // 1- 根据user_key查询ac_user(用户信息),以判断该用户是否存在
        QueryWrapper<AcUser> acUserQueryWrapper = new QueryWrapper<>();
        // lambda表达式 (AcUser acUser) -> {return acUser.getInnerNum();} 转换为 方法引用 AcUser::getInnerNum
        // select(AcUser::getInnerNum) 相当于 "select inner_num from ac_user"
        acUserQueryWrapper.lambda().select(AcUser::getInnerNum).eq(AcUser::getInnerNum, acBillReqDo.getUserKey());
        AcUser existenceAcUser = this.acUserServiceImpl.getOne(acUserQueryWrapper);
        if (TongBeanUtils.isNullObj(existenceAcUser)) {
            // 用户[{}]不存在
            throw TongBusinessUtils.createBusinessException(HaoExceptionEnum.AC_0001.getCode(),
                    String.format(HaoExceptionEnum.AC_0001.getMessage(), acBillReqDo.getUserKey()));
        }

        // 2- 登记"账单记录(ac_bill_flow)"信息
        AcBillFlow acBillFlow = new AcBillFlow();
        acBillFlow.setInnerKey(TongContext.getInstance().getFlowableId());
        acBillFlow.setUserKey(acBillReqDo.getUserKey());
        acBillFlow.setAcDate(acBillReqDo.getAcDate());
        // 交易金额的方向(账单类别编号:01-收入;02-支出;03-转账)
        acBillFlow.setBillClassId(acBillReqDo.getBillClassId());
        // 交易金额的用途(账单类型编号,收入类:午餐,饮料酒水,美妆护肤等;支出类:工资收入,理财收入等)
        acBillFlow.setBillCategoryId(acBillReqDo.getBillCategoryId());
        // 转入账户类型编号
        acBillFlow.setAccountInId(acBillReqDo.getAccountInId());
        // 转出账户类型编号
        acBillFlow.setAccountOutId(acBillReqDo.getAccountOutId());
        acBillFlow.setAcAmount(acBillReqDo.getAcAmount());

        this.acBillFlowServiceImpl.save(acBillFlow);

        // 还需要验证当这个发生异常时，"登记账单记录表"是否会回滚

        // 3- 登记"测试表(ac_test)"信息
        AcTest acTest = new AcTest();
        acTest.setInnerKey(TongContext.getInstance().getFlowableId());
        acTest.setReserveOne("09876543210987654321098765432109876543210987654321");
        this.acTestServiceImpl.save(acTest);

//        try {
//            int i = 1 / 0;
//        } catch (Exception e) {
//            throw TongBusinessUtils.createBusinessException("000", "567890");
//        }
    }

}
